package com.czf.CurriculumVitae.control;

import com.czf.CurriculumVitae.bean.Result;
import com.czf.CurriculumVitae.bean.Specialty;
import com.czf.CurriculumVitae.service.DBService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/v1/specialty/insert")
public class SpecialtyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");

        String userid = request.getParameter("userid");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        Specialty specialty = new Specialty(userid, name, description);
        int row = DBService.insertSpecialty(specialty);
        Result result = null;
        if (row > 0) {
            result = new Result(0, "特长新增成功");
        } else {
            result = new Result(-1, "特长新增失败");
        }
        response.getWriter().append(result.toJSON());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
